#include "matrix.h"
#include <GL/glut.h>
#include <math.h>

//Load matrix to identity
Matrix::Matrix() {
	//Make m identity
	int i;
	for (i = 0; i < 16; i++)
		if (i % 5 == 0)
			m[i] = 1.0f;
		else
			m[i] = 0.0f;
}

//Add translation to matrix
void Matrix::translateAdd(float x, float y, float z)
{
	m[12] += x;
	m[13] += y;
	m[14] += z;
}

//Set translation to matrix
void Matrix::translateSet(float x, float y, float z)
{
	m[12] = x;
	m[13] = y;
	m[14] = z;
}

//Add scale to matrix
void Matrix::scaleAdd(float x, float y, float z)
{
	m[ 0] += x;
	m[ 5] += y;
	m[10] += z;
}

//Set scale to matrix
void Matrix::scaleSet(float x, float y, float z)
{
	m[ 0] = x;
	m[ 5] = y;
	m[10] = z;
}

//Does nothing yet
void Matrix::rotateAdd(float x, float y, float z)
{
}

//This worked better in my head, it only rotates on one axis properly
void Matrix::rotateSet(float x, float y, float z)
{
	if (x)
	{
		//Emulate X rotation
		m[ 5] = cos(x);
		m[ 6] = sin(x);
		m[ 9] = -sin(x);
		m[10] = cos(x);
	}

	if (y)
	{
		//Y rotation
		m[ 0] = cos(y);
		m[ 2] = -sin(y);
		m[ 8] = sin(y);
		m[10] = cos(y);
	}

	if (z)
	{
		//Z rotation
		m[0] = cos(z);
		m[1] = sin(z);
		m[4] = -sin(z);
		m[5] = cos(z);
	}
}

void Matrix::push_apply()
{
	glPushMatrix();
	glMultMatrixf(m);
}

